﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace hxlxc
{
    class IniUtils
    {
        private string Inipath;

        public const int BUFFER_SIZE = 500;

        //public Encoding utf8 = Encoding.GetEncoding("UTF-8");
        //public Encoding defaultEncoding = Encoding.GetEncoding(System.Text.Encoding.Default.HeaderName); // gb2312

        private StringBuilder temp = new StringBuilder(BUFFER_SIZE);

        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

        public IniUtils(string inipath)
        {
            this.Inipath = inipath;
        }

        // 读取ini配置文件
        // [wxg]
        // name = wxg
        // IniReadValue("wxg", "name")
        public string IniReadValue(string Section, string Key)
        {
            temp.Remove(0, temp.Length);
            int i = GetPrivateProfileString(Section, Key, "", temp, BUFFER_SIZE, this.Inipath);
            return temp.ToString();
        }
    }
}
